#include <string>
#include <math.h>
#include <iostream>

using namespace std;

class Solution {
public:
    int myAtoi(string s) {
		long MAX = pow(2, 31) - 1;
        long MIN = pow(-2, 31);
		int i = 0;
		while(s[i] == ' '){
			i++;
		}
		int flag = 1; // 正数
		if(s[i] == '-' || s[i] == '+'){
			if(s[i] == '-'){
				flag = -1;
            }
            i++;
        }
		if(s[i] < 48 || s[i] > 57) return 0;
		long num = 0;
		while(1){
			if(s[i] >= 48 && s[i] <= 57){
				num = num*10 + s[i] - 48;
				i++;
				if(num * flag >= MAX) return MAX;
				if(num * flag <= MIN) return MIN;
			}else{
				break;
			}
		}
		
		return num * flag;
    }
};

int main(){
	Solution so;
	string s = "-+12";

	int num = so.myAtoi(s);
	
	cout << num << endl;
	return 0;
}